
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;
import com.huizhan.web.view.*;

/**
 * 证件类型api
 */
 
@Api(tags = "DocumentType",description = "证件类型")
@RestController
public class DocumentTypeController {
    
    @Autowired
    private DocumentTypeDao dao;
   
    @Autowired
    private DocumentDao documentDao;
    /**
     * 获取证件类型集合
     * @return api结果
     */
    @ApiOperation(value = "获取证件类型集合")
    @RequestMapping(value =  "/api/DocumentType/",method = {RequestMethod.GET})
    public ApiResult<List<DocumentType>> Get(){
         ApiResult<List<DocumentType>> result=new ApiResult<List<DocumentType>>();
        try 
        {
            List<DocumentType> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取证件类型信息
     * @param id 证件类型ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取证件类型信息")
    @RequestMapping(value =  "/api/DocumentType/{id}",method = {RequestMethod.GET})
    public ApiResult<DocumentType> Get(@PathVariable Long id){
        ApiResult<DocumentType> result=new ApiResult<DocumentType>();
        try
        {
            DocumentType model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据项目ID获取证件类型集合
     * @param projectId 项目ID
     * @return api结果
     */
    @ApiOperation(value = "根据项目ID获取证件类型集合")
    @RequestMapping(value =  "/api/DocumentType/Project/{projectId}",method = {RequestMethod.GET})
    public ApiResult<List<DocumentType>> Project(@PathVariable String projectId){
        ApiResult<List<DocumentType>> result=new ApiResult<List<DocumentType>>();
        try
        {
            List<DocumentType> model= dao.findByProjectId(projectId);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }
    
    /**
     * 查询证件类型集合
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询证件类型集合")
    @RequestMapping(value =  "/api/DocumentType/Search",method = {RequestMethod.POST})
    public ApiResult<List<DocumentType>> Search(@RequestBody PageTicketType model){
         ApiResult<List<DocumentType>> result=new ApiResult<List<DocumentType>>();
         
        try
        {    
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"Document_Type_Id") ;            
            Page<DocumentType> page = dao.findByWhere(model.getName(),model.getProjectId(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 修改证件类型信息
     * @param id 证件类型ID
     * @param model 证件类型信息
     * @return api结果
     */
    @ApiOperation(value = "修改证件类型信息")
    @RequestMapping(value =  "/api/DocumentType/{id}",method = {RequestMethod.PUT})
    public ApiResult<DocumentType> Edit(@PathVariable Long id,@RequestBody DocumentType model){
        ApiResult<DocumentType> result=new ApiResult<DocumentType>();
        try
        {
            DocumentType door = dao.findByProjectIdAndName(model.getProjectId(),model.getName());

            if(door!=null&&door.getDocumentTypeId()!=id){
                result.setHasError(true);
                result.setMessage("该名称已经存在");
            }else{
                model.setDocumentTypeId(id);
                dao.save(model);
            }
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加证件类型信息
     * @param model 证件类型信息
     * @return api结果
     */
    @ApiOperation(value = "添加证件类型信息")
    @RequestMapping(value =  "/api/DocumentType/",method = {RequestMethod.POST})
    public ApiResult<DocumentType> Add(@RequestBody DocumentType model){
         ApiResult<DocumentType> result=new ApiResult<DocumentType>();
        try
        {
            if(dao.findByProjectIdAndName(model.getProjectId(),model.getName())==null){
                model.setCreateDate(new Date());
                dao.save(model);
            }else{
                result.setHasError(true);
                result.setMessage("该名称已经存在");
            }
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除证件类型信息
     * @param id 证件类型ID
     * @return api结果
     */
    @ApiOperation(value = "删除证件类型信息")
    @RequestMapping(value =  "/api/DocumentType/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            if(documentDao.countByDocumentTypeId(id)>0){
                result.setHasError(true);
                result.setMessage("该类型下有证件，请先删除子级");
            }else{
                dao.deleteById(id);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

